package handler

import (
	"book/cache"
	"book/model"
	"book/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Loge(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")

	if tel == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}

	redis_key := "pkg_id" + tel
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次禁止登录",
		})
		return
	}

	var list model.User
	err := model.DB.Where("tel = ?", tel).First(&list).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "电话不存在",
		})
		return
	}

	if list.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "电话id不存在",
		})
		return
	}

	if pkg.Md5(password) != list.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,禁止登录2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": list,
	})
	return
}
func RoleNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "id不能为空",
		})
		return
	}

	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": &data,
	})
	return
}
func RoleUser(c *gin.Context) {
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.rid
		WHERE
		  user_roles.uid = 1
	*/
	uid := c.Query("uid")
	var list model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid = ?", uid).
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func RoleNode(c *gin.Context) {
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.rid
		WHERE
		  role_nodes.nid = 1
	*/
	nid := c.Query("nid")
	var list model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.rid").
		Where("role_nodes.nid = ?", nid).
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func BookAdd(c *gin.Context) {
	bookno := c.PostForm("bookno")
	bookname := c.PostForm("bookname")
	bookasd := c.PostForm("bookasd")

	if bookno == "" || bookname == "" || bookasd == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}

	data := model.Book{
		BookNo:   bookno,
		BookName: bookname,
		BookAsd:  bookasd,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": &data,
	})
	return
}
func BookListAdd(c *gin.Context) {
	bid, _ := strconv.Atoi(c.PostForm("bid"))
	booklistno := c.PostForm("booklistno")
	booklistname := c.PostForm("booklistname")
	booklistlx := c.PostForm("booklistlx")

	if bid == 0 || booklistno == "" || booklistname == "" || booklistlx == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}

	data := model.BookList{
		Bid:          bid,
		BookListNo:   booklistno,
		BookListName: booklistname,
		BookListLx:   booklistlx,
	}

	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": &data,
	})
	return
}
func BookListDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	/*
		SELECT
		book_lists.bid AS Bid,
		  books.book_no AS BookNo,
		  books.book_name AS BookName,
		  books.book_asd AS BookAsd,
		  book_lists.book_list_no AS BookListNo,
		  book_lists.book_list_name AS BookListName,
		  book_lists.book_list_lx AS BookListLx
		FROM
		  books
		  LEFT JOIN book_lists ON books.id = book_lists.bid
	*/
	offset := (page - 1) * size
	var list []model.BookDetail
	reids_key := "list_id" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, reids_key).Result()

	if cacheData == "" {

		model.DB.Table("books").
			Select(
				"book_lists.bid AS Bid",
				"books.book_no AS BookNo",
				"books.book_name AS BookName",
				"books.book_asd AS BookAsd",
				"book_lists.book_list_no AS BookListNo",
				"book_lists.book_list_name AS BookListName",
				"book_lists.book_list_lx AS BookListLx",
			).
			Joins("LEFT JOIN book_lists ON books.id = book_lists.bid").
			Limit(size).
			Offset(offset).
			Find(&list).
			Debug()
		jsonData, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, reids_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	model.DB.Model(model.BookList{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
	})
	return
}
func BookList(c *gin.Context) {

	/*
		SELECT
		book_lists.bid AS Bid,
		  books.book_no AS BookNo,
		  books.book_name AS BookName,
		  books.book_asd AS BookAsd,
		  book_lists.book_list_no AS BookListNo,
		  book_lists.book_list_name AS BookListName,
		  book_lists.book_list_lx AS BookListLx
		FROM
		  books
		  LEFT JOIN book_lists ON books.id = book_lists.bid
	*/
	id := c.Query("id")
	var list []model.BookDetail
	reids_key := "id_id" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, reids_key).Result()
	if cacheData == "" {
		model.DB.Table("books").
			Select(
				"book_lists.bid AS Bid",
				"books.book_no AS BookNo",
				"books.book_name AS BookName",
				"books.book_asd AS BookAsd",
				"book_lists.book_list_no AS BookListNo",
				"book_lists.book_list_name AS BookListName",
				"book_lists.book_list_lx AS BookListLx",
			).
			Joins("LEFT JOIN book_lists ON books.id = book_lists.bid").
			Where("book_lists.bid = ?", id).
			Find(&list).
			Debug()
		jsonData, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, reids_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	model.DB.Model(model.BookList{}).Count(&total)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
	return
}
